package com.hontech.bvapp2.ui.parts.drop

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Drop
import com.hontech.bvapp2.ui.view.LabelState
import com.hontech.bvapp2.ui.view.LabelTextView
import com.hontech.bvapp2.utils.registerEvent
import com.hontech.bvapp2.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DropStatusParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val lsDropSw1: LabelState
    private val lsDropSw2: LabelState
    private val lsDropSw3: LabelState
    private val lsFilmSw: LabelState
    private val lsFilmChecked: LabelState
    private val lsCutTop: LabelState
    private val lsCutBottom: LabelState
    private val lsCutChecked: LabelState
    private val lsDropChecked1: LabelState
    private val lsDropChecked2: LabelState
    private val lsDropChecked3: LabelState
    private val lsLeak: LabelState
    private val lsWaste: LabelState
    private val lsCupChecked: LabelState

    private val ltAnalog: LabelTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_drop_status, this)
        lsDropSw1 = view.findViewById(R.id.id_drop_status_drop_sw1)
        lsDropSw2 = view.findViewById(R.id.id_drop_status_drop_sw2)
        lsDropSw3 = view.findViewById(R.id.id_drop_status_drop_sw3)
        lsFilmSw = view.findViewById(R.id.id_drop_status_film_sw)
        lsFilmChecked = view.findViewById(R.id.id_drop_status_film_checked)
        lsCutTop = view.findViewById(R.id.id_drop_status_cut_top)
        lsCutBottom = view.findViewById(R.id.id_drop_status_cut_bottom)
        lsCutChecked = view.findViewById(R.id.id_drop_status_cut_checked)
        lsDropChecked1 = view.findViewById(R.id.id_drop_status_drop_checked1)
        lsDropChecked2 = view.findViewById(R.id.id_drop_status_drop_checked2)
        lsDropChecked3 = view.findViewById(R.id.id_drop_status_drop_checked3)
        lsLeak = view.findViewById(R.id.id_drop_status_leak)
        lsWaste = view.findViewById(R.id.id_drop_status_waste)
        ltAnalog = view.findViewById(R.id.id_drop_status_adc)
        lsCupChecked = view.findViewById(R.id.id_drop_status_cup_checked)
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onStatusEvent(status: Drop.Status) {
        lsDropSw1.set(status.isDropSw1())
        lsDropSw2.set(status.isDropSw2())
        lsDropSw3.set(status.isDripSw3())
        lsFilmSw.set(status.isFilmSw())
        lsFilmChecked.set(status.isFilmCupChecked())
        lsCutTop.set(status.isCutTop())
        lsCutBottom.set(status.isCutBottom())
        lsCutChecked.set(status.isCutChecked())
        lsDropChecked1.set(status.isDropChecked1())
        lsDropChecked2.set(status.isDropChecked2())
        lsDropChecked3.set(status.isDropChecked3())
        lsLeak.set(status.isLeak())
        lsWaste.set(status.isWaste())
        ltAnalog.set(status.analog)
        lsCupChecked.set(status.isCupChecked())
    }
}















